﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using LibraryO2S;
using BusinessObjects;

public partial class controls_admin_ArticleComments : Pages_Base
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        try
        {
            loadComment();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    private void loadComment()
    {
        try
        {
            DataTable objDT = BO_ArticleComments.SelectWithPaged(base.Cur_page, base.Page_size, 1);
            module_title.InnerText = "Danh sách bình luận";//BO_Articles.Search(a_id).Rows[0]["FULL_ARTICLE_TITLE"].ToString();
            bindRepeater(rptContact, objDT, pagerContainer, divMessage, divResult);
        }
        catch (Exception ex)
        {
            //throw ex;
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi trong quá trình xử lý<br />" + ex.Message);
        }
    }
    protected void rptContact_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            string commandName = e.CommandName;
            if (commandName.Equals("del"))
            {
                //int ac_id = Convert.ToInt32(e.CommandArgument);
                //if (LibraryO2S.O2S_SqlUtils.ExecuteNonQuery("delete from articlecomments where comment_id=" + ac_id) > 0)
                //    divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Xóa thành công .");
                //loadComment();
            }
            else if (commandName.Equals("status"))
            {
                int ac_id = Convert.ToInt32(e.CommandArgument);
                DataTable objDT = BO_ArticleComments.Search(ac_id);
                if (objDT.Rows.Count > 0)
                {
                    BO_ArticleComments.Update(long.Parse(objDT.Rows[0]["COMMENT_ID"].ToString()), long.Parse(objDT.Rows[0]["ARTICLE_ID"].ToString()), objDT.Rows[0]["EMAIL"].ToString(), objDT.Rows[0]["FULL_NAME"].ToString(), objDT.Rows[0]["MOBILE"].ToString(), objDT.Rows[0]["ADDRESS"].ToString(), objDT.Rows[0]["DETAIL"].ToString(), Int32.Parse(objDT.Rows[0]["SORT_DISPLAY"].ToString()), Convert.ToDateTime(objDT.Rows[0]["CREATED_DATE"].ToString()), Convert.ToDateTime(objDT.Rows[0]["MODIFY_DATE"].ToString()), objDT.Rows[0]["MODIFY_BY"].ToString(), !Convert.ToBoolean(objDT.Rows[0]["STATUS"].ToString()));
                }
                divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Cập nhật thay đổi thành công!");
                loadComment();
            }
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi: <br />" + ex.Message);
        }
    }
    protected void rptContact_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Button btnStatus = (Button)e.Item.FindControl("btnStatus");

        bool isdetail = Convert.ToBoolean(btnStatus.ToolTip);
        if (!isdetail)
            btnStatus.CssClass = "icon-32-cancel";
        else
            btnStatus.CssClass = "icon-32-checking";
    }
    protected void chbxall_CheckedChanged(object sender, EventArgs e)
    {
        if (chbxall.Checked)
        {
            foreach (RepeaterItem item in rptContact.Items)
            {
                ((CheckBox)item.FindControl("chbx")).Checked = true;
            }
        }
        else
        {
            foreach (RepeaterItem item in rptContact.Items)
            {
                ((CheckBox)item.FindControl("chbx")).Checked = false;
            }
        }
    }
    protected void ddlAction_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlAction.SelectedIndex == 1)
        {
            foreach (RepeaterItem item in rptContact.Items)
            {
                CheckBox cb = ((CheckBox)item.FindControl("chbx"));
                if (cb.Checked)
                {
                    int ac_id = Convert.ToInt32(cb.ToolTip);
                    DataTable objDT = BO_ArticleComments.Search(ac_id);
                    if (objDT.Rows.Count > 0)
                    {
                        BO_ArticleComments.Update(long.Parse(objDT.Rows[0]["COMMENT_ID"].ToString()), long.Parse(objDT.Rows[0]["ARTICLE_ID"].ToString()), objDT.Rows[0]["EMAIL"].ToString(), objDT.Rows[0]["FULL_NAME"].ToString(), objDT.Rows[0]["MOBILE"].ToString(), objDT.Rows[0]["ADDRESS"].ToString(), objDT.Rows[0]["DETAIL"].ToString(), Int32.Parse(objDT.Rows[0]["SORT_DISPLAY"].ToString()), Convert.ToDateTime(objDT.Rows[0]["CREATED_DATE"].ToString()), Convert.ToDateTime(objDT.Rows[0]["MODIFY_DATE"].ToString()), objDT.Rows[0]["MODIFY_BY"].ToString(), !Convert.ToBoolean(objDT.Rows[0]["STATUS"].ToString()));
                    }
                }
            }
        }
        if (ddlAction.SelectedIndex == 2)
        {
            foreach (RepeaterItem item in rptContact.Items)
            {
                CheckBox cb = ((CheckBox)item.FindControl("chbx"));
                if (cb.Checked)
                {
                    //int ac_id = Convert.ToInt32(cb.ToolTip);
                    //LibraryO2S.O2S_SqlUtils.ExecuteNonQuery("delete from articlecomments where comment_id=" + ac_id);
                }
            }
            divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Xóa thành công .");
        }
        chbxall.Checked = false;
        ddlAction.SelectedIndex = 0;
        loadComment();
    }
}
